package com.kws.system.util;


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;



/**
 * <b>ThreeDESUtil</b>
 *
 * @description: ThreeDESUtil <br>
 * @date: 2024/12/10 17:27 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
public class ThreeDESUtil {
    /**
     * 加解密统一编码方式
     */
    private final static String ENCODE = "UTF-8";

    /**
     * 加密方式
     */
    private final  static String KEY_ALGORITHM = "DESede";

    /**
     * 加密具体填充方式
     */
    private final static String KEY_ALGORITHM_PADDING = "DESede/ECB/PKCS5Padding";


    /**
     * 3DES加密
     * @param message
     * @param key
     * @return
     * @throws Exception
     */
    public static String encode(String message,String key) throws Exception {
        byte[] keyByte = key.getBytes();
        SecretKey sk = new SecretKeySpec(keyByte, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, sk);
        return CommonUtil.toHexString(cipher.doFinal(message.getBytes(ENCODE)));
//        try {
//            DESedeKeySpec spec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));
//            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(ALGORITHM);
//            Key deskey = keyfactory.generateSecret(spec);
//            Cipher cipher = Cipher.getInstance(PATTERN);
//            cipher.init(Cipher.ENCRYPT_MODE, deskey);
//            byte[] bOut = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
//            return Base64.encodeBase64String(bOut);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("3DES 解密错误");
//        }
    }


    /**
     * 3DES解密
     * @param encodeMessage
     * @param key
     * @return
     * @throws Exception
     */
//    public static String decode(String encodeMessage,String key) throws Exception {
//        SecretKey secretKey = new SecretKeySpec(key.getBytes(DNCODING),ALGORITHM);
//        Cipher cipher = Cipher.getInstance(PATTERN);
//        cipher.init(Cipher.DECRYPT_MODE,secretKey);
//        byte[] decryptData = cipher.doFinal(Base64.getDecoder().decode(encodeMessage));
//        return new String(decryptData,DNCODING);
//    }


    public static String getSign(String param) throws Exception {
        MessageDigest messageDigest;
        messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(param.getBytes(ENCODE));
        return CommonUtil.toHexString(messageDigest.digest());
    }

    public static void main(String[] args) throws Exception {
        String str = "章三、李四、王二";
//        String encode = encode(str, "2012PinganVitality075522");
//        System.out.println(encode);
//        String decode = decode(encode, "2012PinganVitality075522");
//        System.out.println(decode);

        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(str.getBytes(ENCODE));
            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            String sign = hexString.toString();

            System.out.println(sign);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
